package com.yue.spdatasource.aspect;

import com.yue.spdatasource.annotation.DsType;
import com.yue.spdatasource.datasource.DynamicDataSourceContextHolder;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class DsTypeAspect {
    @Before("@annotation(dsType)")
    public void setDsType(DsType dsType) {
        // 切换数据源
        DynamicDataSourceContextHolder.setDataSourceType(dsType.value());
    }
    @After("@annotation(dsType)")
    public void clearDsType(DsType dsType) {
        // 切换数据源
        DynamicDataSourceContextHolder.clearDataSourceType();
    }
}
